//
//  PywPassportRegisterApi.m
//  PywSdk
//
//  Created by smallwave on 16/8/5.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywPassportRegisterApi.h"
#import "AIFPassportRegisterManager.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"

@interface PywPassportRegisterApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywPassportRegisterApi

- (void)requestRegisterWithParams:(MutableDictionary *)params
                          success:(Successd)success
                             fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    
    AIFPassportRegisterManager *manager = [[AIFPassportRegisterManager alloc] init];
    manager.delegate = self;
    manager.passport = [params objectForKey:@"passport"];
    manager.password = [params objectForKey:@"password"];
    manager.smsType = [params objectForKey:@"smstype"];
    manager.is_check_smscode = [params objectForKey:@"is_check_smscode"];
    [manager loadData];
}

#pragma mark - ETAPIManagerCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    [self registerWithDict:dict];
}
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    [self registerFail];
}

/**
 *  注册成功
 *
 *  @param dict <#dict description#>
 */
- (void)registerWithDict:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    
    switch (resultCode) {
        case 200:
        {
            [PywMBProgressHUD showSuccess:@"注册成功！" toView:[UIApplication sharedApplication].keyWindow];
            
            NSString *account = [NSString stringWithFormat:@"%@",dict[@"data"][@"game_account"][@"username"]];
            if (self.successd) {
                self.successd(nil,account);
            }
        }
            break;
            
        default:
        {
            NSString *msg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:msg toView:[UIApplication sharedApplication].keyWindow];
            [self registerFail];
        }
            break;
    }
}
/**
 *  注册失败
 */
- (void)registerFail
{
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

@end
